package org.example.xiaomi.util;

import org.apache.commons.beanutils.*;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


public class MyBeanUtils {
    //注册日期转换类
    static {
        ConvertUtils.deregister(Date.class);
        ConvertUtils.register(new Converter() {
            @Override
            public  Object convert(Class type, Object value) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return  sdf.parse(String.valueOf(value));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }
    public static void populate(Object bean, Map<String, ? extends Object> properties)  {
        try {
            BeanUtilsBean.getInstance().populate(bean, properties);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}